// https://leetcode.cn/problems/happy-number/

class Solution {
public:
    int slove(int t) {
        int sum = 0;
        while(t){
            int temp = t % 10;
            sum += temp * temp;
            t /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int a = slove(n);
        int b = slove(slove(n));
        while(true) {
            if(a == 1 || b == 1) {
                return true;
            }
            if(a == b){
                return false;
            }
            a = slove(a);
            b = slove(slove(b));
        }
    }
};


class Solution {
public:
    int transform(int x)
    {
        int sum=0;
        while(x)
        {
            int t=x%10;
            sum+=t*t;
            x/=10;
        }
        return sum;
    }
    bool isHappy(int n) {
        int slow=n,fast=transform(n);
        while(slow!=fast)
        {
            slow=transform(slow);
            fast=transform(transform(fast));
        }
        return slow==1;
    }
};